package org.itboys.ace.web;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.entity.AdminUser;
import org.itboys.admin.tools.AdminCookieHolder;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.commons.utils.encryption.Digests;
import org.itboys.commons.utils.time.TimeUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.manage.service.ManagerUserService;
import org.itboys.seller.constant.CaliforniaSiteConstant;
import org.itboys.seller.dto.MatchRecordVO;
import org.itboys.seller.dto.SellerUserVO;
import org.itboys.seller.dto.SiteUserVO;
import org.itboys.seller.entity.Seller;
import org.itboys.seller.entity.SellerUser;
import org.itboys.seller.entity.SiteUser;
import org.itboys.seller.meta.MatchInfo;
import org.itboys.seller.service.AccountService;
import org.itboys.seller.service.CaliforniaSiteUserService;
import org.itboys.seller.service.SellerService;
import org.itboys.seller.service.SellerUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hzliangchong1@corp.netease.com
 *         Date 2016/10/22.
 */
@RestController
@RequestMapping("/manage")
public class ManageController extends BaseController {

    @Autowired
    private ManagerUserService managerUserService;

    @Autowired
    private SellerUserService sellerUserService;

    @Autowired
    private AccountService accountService;

    /**
     * 进入注册页面
     */
    @RequestMapping("/reg")
    public ModelAndView login(Model model,HttpServletRequest request){
        return new ModelAndView("manage/reg");

    }

    /**
     * 进入基本信息页
     */
    @RequestMapping("/toBaseInfoVM")
    public ModelAndView toBaseInfoVM(Model model,HttpServletRequest request) {
        String userName = AdminCookieHolder.getAdminUserName();
        AdminUser adminUser = managerUserService.getAdminUser(userName);
        model.addAttribute("userInfo", adminUser);
        return new ModelAndView("manage/userBaseInfo");
    }

    /**
     * 更新信息
     */
    @RequestMapping("/editManage")
    public void editManage(@ModelAttribute AdminUser user,
                           HttpServletRequest request,HttpServletResponse response) {
        AdminUser proUser = managerUserService.getAdminUser(user.getUsername());
        if (proUser == null) {
            AjaxUtils.renderText(response, "用户不存在或已被冻结");
            return;
        }
        proUser.setRealName(user.getRealName());
        proUser.setPhone(user.getPhone());
        proUser.setIdCard(user.getIdCard());
        proUser.setCompanyName(user.getCompanyName());
        managerUserService.update(proUser);
        AjaxUtils.renderText(response, CommonConstants.SUCCESS);

    }

    /**
     * 进入修改密码页
     */
    @RequestMapping("/toPassWordXg")
    public ModelAndView toPassWordXg(Model model,HttpServletRequest request) {
        return new ModelAndView("manage/password-xg");
    }

    /**
     * 更新信息
     */
    @RequestMapping("/updateManagePassword")
    public void updateSellerPassword(String oldPassword, String password,
                           HttpServletRequest request,HttpServletResponse response) {
        String userName = AdminCookieHolder.getAdminUserName();
        AdminUser proUser = managerUserService.getAdminUser(userName);
        if (proUser == null) {
            AjaxUtils.renderText(response, "用户不存在或已被冻结");
            return;
        }
        if(!StringUtils.equals(proUser.getPassword(), Digests.md5(oldPassword))){
            AjaxUtils.renderText(response, "-1");//密码不正确
            return ;
        }
        proUser.setPassword(Digests.md5(password));
        managerUserService.update(proUser);
        AjaxUtils.renderText(response, CommonConstants.SUCCESS);

    }

    /**
     * 注册
     */
    @RequestMapping("/regSeller")
    public void regMember(@ModelAttribute SellerUser user,
                          HttpServletRequest request,HttpServletResponse response) {
        SellerUser proUser = sellerUserService.getAdminUserByPhone(user.getPhone());
        if (proUser != null) {
            AjaxUtils.renderText(response, CommonConstants.EXISTED);
            return;
        }
        sellerUserService.insert(user);
        AjaxUtils.renderText(response, CommonConstants.SUCCESS);

    }

    /**
     * 更新信息
     */
    @RequestMapping("/editSeller")
    public void editSeller(@ModelAttribute SellerUser user,
                          HttpServletRequest request,HttpServletResponse response) {
        SellerUser proUser = sellerUserService.getAdminUser(user.getUserName());
        if (proUser == null) {
            AjaxUtils.renderText(response, "用户不存在或已被冻结");
            return;
        }
        SellerUser phoneUser = sellerUserService.getAdminUserByPhone(user.getPhone());
        if (phoneUser != null && !proUser.getUserName().equals(user.getUserName())) {
            AjaxUtils.renderText(response, CommonConstants.EXISTED);
            return;
        }
        proUser.setRealName(user.getRealName());
        proUser.setPhone(user.getPhone());
        proUser.setIdCard(user.getIdCard());
        proUser.setCompanyName(user.getCompanyName());
        sellerUserService.update(proUser);
        AjaxUtils.renderText(response, CommonConstants.SUCCESS);

    }

    @RequestMapping("/mySellerUserList")
    public void indexAllList(int pageNo, int pageSize,
                             HttpServletRequest request, HttpServletResponse response) {
        String adminUserName= AdminCookieHolder.getAdminUserName();
        int offset = Math.max(0, (pageNo - 1) * pageSize);
        List<SellerUser> sellerUserList = sellerUserService.queryInfo(offset, pageSize);
        List<SellerUserVO> voList = sellerUserList.stream().map(p -> {
            SellerUserVO vo = new SellerUserVO();
            vo.setCompanyName(p.getCompanyName());
            vo.setPhone(p.getPhone());
            vo.setUserName(p.getUserName());
            vo.setRealName(p.getRealName());
            vo.setIdCard(p.getIdCard());
            return vo;
        }).collect(Collectors.toList());
        JsonPageUtils.renderJsonPage(sellerUserService.totalInfo(), voList, response);
    }
}
